package com.example.httpurlconnectiondemo;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private Button btnSend;
    private EditText etUrl;
    private static TextView textView;
    private static Handler handler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        handler=new MyHandler();
        etUrl=(EditText) findViewById(R.id.et_Url);
        textView=(TextView) findViewById(R.id.textView);
        textView.setText("");
        btnSend = (Button) findViewById(R.id.btn_Send);
        btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sendRequest();
            }
        });
    }
    private void sendRequest() {
        new Thread(new Runnable() {    //开启子线程
            @Override
            public void run() {  //网络数据传输比较耗时，在子线程中进行
                HttpURLConnection conn = null; // 获取HttpURLConnection的实例
                try {
                    URL url = new URL(etUrl.getText().toString());
                    conn = (HttpURLConnection) url.openConnection();
                    conn.setRequestMethod("GET"); // 设置HTTP请求方式
                    conn.setConnectTimeout(8000); // 自由的定制，设置连接超时、读取超时的毫秒数
                    conn.setReadTimeout(8000);
                    InputStream in = conn.getInputStream(); // 获取到服务器返回的输入流
                    BufferedReader reader = new BufferedReader(new InputStreamReader(in)); // 对输入流进行读取
                    StringBuilder strBuilder = new StringBuilder();
                    String strLine;
                    while ((strLine = reader.readLine()) != null) {
                        strBuilder.append(strLine);
                    }
                    Log.d(TAG, strBuilder.toString());
                    //发送message到主线程
                    Message msg=handler.obtainMessage();
                    msg.obj=strBuilder.toString();
                    handler.sendMessage(msg);
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (conn != null) { // 将HTTP连接关闭
                        conn.disconnect();
                    }
                }
            }
        }).start();
    }
    public static class MyHandler extends  Handler{
        @Override
        public void handleMessage(@NonNull Message msg) { //接收子线程发来的msg
            super.handleMessage(msg);
            textView.setText(msg.obj.toString()); //利用收到的消息更新TextView
        }
    };
}

